package com.atguigu.order.web;

import com.alipay.api.AlipayApiException;
import com.atguigu.order.config.AlipayTemplate;
import com.atguigu.order.service.OrderService;
import com.atguigu.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * description: PayWebController
 * date: 2023/1/1 16:36
 * author: MR.孙
 */
@Controller
public class PayWebController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private AlipayTemplate alipayTemplate;

    /**
     * 1.将支付页让浏览器展示
     * 2.支付成功以后，我们要调到用户的订单列表页
     * @param orderSn
     * @return
     * @throws AlipayApiException
     */
    @GetMapping(value = "/payOrder", produces = MediaType.TEXT_HTML_VALUE)
    @ResponseBody
    public String payOrder(@RequestParam("orderSn")  String orderSn) throws AlipayApiException {

        PayVo payVo = orderService.getOrderPay(orderSn);
        String result = alipayTemplate.pay(payVo);

        return result;

    }

}
